/**
 * @author		Ben Layet
 * @version 	1.0
 * @date		24 Sep 2008
 */
package org.ignoramus.application.model;

import java.util.Map;


/**
 * Responsible for creating review cards.
 */
public class CardFactory
{
	/** The template of the card. */
	private final CardTemplate template;

	/**
	 * Sole constructor.
	 * 
	 * @param template The template of the card. 
	 */
	public CardFactory(CardTemplate template)
	{
		this.template = template;
	}

	/**
	 * Create a card from a map of its content.
	 * @param cardContent the map of the card content (info type to info value)
	 * @return a new review card.
	 */
	public ReviewCard createReviewCard(Map<String, String> cardContent)
	{
		//the return value
		ReviewCardImpl ret = new ReviewCardImpl();
		//set the card title
		ret.setTitle(cardContent.get(template.getTitleSource()));
		
		/** A ReviewCard contains all the elements defined by the template even though some might be disabled */
		
		//loop through the template elements
		for (String infoType : template.keySet())
		{
			//get the element template
			CardTemplateElement templateElmt = template.get(infoType);
			//get the value that can be null
			String value = cardContent.get(infoType);
			//create the card element
			CardElement element = new CardElement(templateElmt, value);
			
			//add the element to the card
			ret.addElement(infoType, element);
		}
		
		//return the card
		return ret;
	}
}
